import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import Home from '@/views/home/Home'
import ProductList from '@/views/product/Items'
import JifenShop from '@/views/product/JifenShop'
import JifenShopAll from '@/views/product/JifenShopAll'
import Product from '@/views/product/Product'
import Promotion from '@/views/product/Promotion'
import Cart from '@/views/product/Cart'
import User from '@/views/user/Index'
import Coupon from '@/views/user/Coupon'
import Level from '@/views/user/Level'
import Address from '@/views/address/Address'
import Checkout from '@/views/order/Checkout'
import Order from '@/views/order/Order'
import OrderGoods from '@/views/order/OrderGoods'
import Comment from '@/views/product/Comment'
import CommentList from '@/views/product/CommentList'
import Collection from '@/views/user/Collection'
import History from '@/views/user/History'
import ContactUs from '@/views/user/ContactUs'
import Fadeback from '@/views/user/Fadeback'
import Integral from '@/views/user/Integral'
import Integral2 from '@/views/user/Integral2'
import Balance from '@/views/user/Balance'
import Group from '@/views/user/Group'
import Share from '@/views/user/Share'
import Donation from '@/views/donation/Donation'
import DonationRank from '@/views/donation/Rank'
import Donate from '@/views/donation/Donate'
import CourseList from '@/views/course/Items'
import Teachers from '@/views/course/Teachers'
import Teacher from '@/views/course/Teacher'
import Chat from '@/views/course/Chat'
import Message from '@/views/user/Message'
import Catalog from '@/views/course/Catalog'
import Courses from '@/views/course/Courses'
import CourseCategory from '@/views/course/Category'
import PostList from '@/views/post/Items'
import Post from '@/views/post/Post'
import Login from '@/views/auth/Login'
import ShareCard from '@/views/share/Card'
import Welcome from '@/views/Welcome/Welcome'
import PositionPage from '@/views/PositionPage/PositionPage'
import MyPage from '@/views/MyPage/MyPage'
import NewsPage from '@/views/NewsPage/NewsPage'
import PostNews from '@/views/NewsPage/PostNews'
import TalentPage from '@/views/TalentPage/TalentPage'
import Joblist from '@/views/TalentPage/Joblist'
import CompanyList from '@/views/TalentPage/CompanyList'
import Workplacecredit from '@/views/MyPage/Workplacecredit'
import Applyfortherecord from '@/views/MyPage/Applyfortherecord'
import Ifocuson from '@/views/MyPage/Ifocuson'
import Information from '@/views/PositionPage/Information'
import Questionsinfor from '@/views/PositionPage/Questionsinfor'
import Attentionps from '@/views/PositionPage/Attentionps'
import Myresume from '@/views/MyPage/Myresume'
import Radarmap from '@/views/MyPage/Radarmap'
import Recommendation from '@/views/PositionPage/Recommendation'
import Companyhomepage from '@/views/PositionPage/Companyhomepage'
import Talents from '@/views/PositionPage/Talents'
import Worktlist from '@/views/PositionPage/Worktlist'
import Issue from '@/views/PositionPage/Issue'
import Companydetails from '@/views/PositionPage/Companydetails'
import previewLouw from '@/views/PositionPage/previewLouw'
import Paixielit from '@/views/PositionPage/Paixielit'
import Myassessment from '@/views/MyPage/Myassessment'
import Myvip from '@/views/MyPage/Myvip'
import Details from '@/views/MyPage/Details'
import WhoLookMy from '@/views/MyPage/WhoLookMy'
import Invite from '@/views/MyPage/Invite'
import Historical from '@/views/MyPage/Historical'
import Addresume from '@/views/MyPage/Addresume'
import MyReleasePosition from '@/views/MyPage/MyReleasePosition'
import StoreBasic from '@/views/MyPage/StoreBasic'
import Search from '@/views/components/Search'
import Jobsearch from '@/views/components/Jobsearch'
import Companysearch from '@/views/components/Companysearch'
import Answer from '@/views/MyPage/Answer'
import Workadd from '@/views/MyPage/Workadd'
import Intention from '@/views/MyPage/Intention'
import Project from '@/views/MyPage/Project'
import Preview from '@/views/MyPage/Preview'
import Privacy from '@/views/MyPage/Privacy'
import Corporate from '@/views/MyPage/Corporate'
import Evaluation from '@/views/MyPage/Evaluation'
import JoinVip from '@/views/MyPage/JoinVip'
import Assessment from '@/views/MyPage/Assessment'
import Positionchange from '@/views/MyPage/Positionchange'
import Checkitout from '@/views/MyPage/Checkitout'
import Collect from '@/views/MyPage/Collect'
import Changes from '@/views/MyPage/Changes'
import CompanyUpdata from '@/views/MyPage/CompanyUpdata'
import Teamgoum from '@/views/MyPage/Teamgoum'
import Forum from '@/views/forum/Item'
import Particulars from '@/views/forum/Particulars'
import PostsBbs from '@/views/forum/PostsBbs'
import Comments from '@/views/forum/Comment'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/',
      name: 'home',
      meta: { requiresAuth: true },
      component: Home
    },
    {
      path: '/products',
      name: 'productList',
      meta: { requiresAuth: true },
      component: ProductList
    },
    {
      path: '/jifen_shop_all',
      name: 'JifenShop',
      meta: { requiresAuth: true },
      component: JifenShopAll
    },
    {
      path: '/jifen_shop',
      name: 'JifenShop',
      meta: { requiresAuth: true },
      component: JifenShop
    },
    {
      path: '/products/:id',
      name: 'product',
      component: Product
    },
    {
      path: '/promotions/:type',
      name: 'promotion',
      meta: { requiresAuth: true },
      component: Promotion
    },
    {
      path: '/carts',
      name: 'cart',
      meta: { requiresAuth: true },
      component: Cart
    },
    {
      path: '/user',
      name: 'user',
      meta: { requiresAuth: true },
      component: User
    },
    {
      path: '/messages',
      name: 'message',
      meta: { requiresAuth: true },
      component: Message
    },
    {
      path: '/level',
      name: 'level',
      meta: { requiresAuth: true },
      component: Level
    },
    {
      path: '/share',
      name: 'share',
      meta: { requiresAuth: true },
      component: Share
    },
    {
      path: '/sharecard/:src',
      name: 'sharecard',
      meta: { requiresAuth: false },
      component: ShareCard
    },
    {
      path: '/collections',
      name: 'collection',
      meta: { requiresAuth: true },
      component: Collection
    },
    {
      path: '/coupons',
      name: 'coupon',
      meta: { requiresAuth: true },
      component: Coupon
    },
    {
      path: '/particulars',
      name: 'Particulars',
      meta: { requiresAuth: true },
      component: Particulars
    },
    {
      path: '/postsBbs',
      name: 'PostsBbs',
      meta: { requiresAuth: true },
      component: PostsBbs
    },
    {
      path: '/comments',
      name: 'Comments',
      meta: { requiresAuth: true },
      component: Comments
    },
    {
      path: '/forum',
      name: 'forum',
      meta: { requiresAuth: true },
      component: Forum
    },
    {
      path: '/search',
      name: 'Search',
      meta: { requiresAuth: true },
      component: Search
    },
    {
      path: '/jobsearch',
      name: 'Jobsearch',
      meta: { requiresAuth: true },
      component: Jobsearch
    },
    {
      path: '/companysearch',
      name: 'Companysearch',
      meta: { requiresAuth: true },
      component: Companysearch
    },
    {
      path: '/history',
      name: 'history',
      meta: { requiresAuth: true },
      component: History
    },
    {
      path: '/contact_us',
      name: 'contactUs',
      meta: { requiresAuth: true },
      component: ContactUs
    },
    {
      path: '/fadeback',
      name: 'fadeback',
      meta: { requiresAuth: true },
      component: Fadeback
    },
    {
      path: '/integrals',
      name: 'integral',
      meta: { requiresAuth: true },
      component: Integral
    },
    {
      path: '/integrals2',
      name: 'integral2',
      meta: { requiresAuth: true },
      component: Integral2
    },
    {
      path: '/balances',
      name: 'balance',
      meta: { requiresAuth: true },
      component: Balance
    },
    {
      path: '/group',
      name: 'group',
      meta: { requiresAuth: true },
      component: Group
    },
    {
      path: '/addresses',
      name: 'address',
      meta: { requiresAuth: true },
      component: Address
    },
    {
      path: '/donation',
      name: 'donation',
      meta: { requiresAuth: true },
      component: Donation
    },
    {
      path: '/donate',
      name: 'donate',
      meta: { requiresAuth: true },
      component: Donate
    },
    {
      path: '/donation/rank',
      name: 'donationRank',
      meta: { requiresAuth: true },
      component: DonationRank
    },
    {
      path: '/courses',
      name: 'course',
      meta: { requiresAuth: true },
      component: Courses
    },
    {
      path: '/teachers',
      name: 'teachers',
      meta: { requiresAuth: true },
      component: Teachers
    },
    {
      path: '/teachers/:teacher',
      name: 'teacher',
      meta: { requiresAuth: true },
      component: Teacher
    },
    {
      path: '/chat/:user',
      name: 'chat',
      meta: { requiresAuth: true },
      component: Chat
    },
    {
      path: '/courses/category',
      name: 'courseCategory',
      meta: { requiresAuth: true },
      component: CourseCategory
    },
    {
      path: '/courses/list',
      name: 'courseList',
      meta: { requiresAuth: true },
      component: CourseList
    },
    {
      path: '/courses/:course',
      name: 'catalog',
      meta: { requiresAuth: true },
      component: Catalog
    },
    {
      path: '/posts',
      name: 'postList',
      meta: { requiresAuth: true },
      component: PostList
    },
    {
      path: '/posts/:post',
      name: 'post',
      meta: { requiresAuth: true },
      component: Post
    },
    {
      path: '/welcome',
      name: 'welcome',
      meta: { requiresAuth: true },
      component: Welcome
    },
    {
      path: '/PositionPage',
      name: 'positionPage',
      meta: { requiresAuth: true },
      component: PositionPage
    },
    {
      path: '/myPage',
      name: 'myPage',
      meta: { requiresAuth: true },
      component: MyPage
    },
    {
      path: '/workadd',
      name: 'Workadd',
      meta: { requiresAuth: true },
      component: Workadd
    },
    {
      path: '/intention',
      name: 'Intention',
      meta: { requiresAuth: true },
      component: Intention
    },
    {
      path: '/project',
      name: 'Project',
      meta: { requiresAuth: true },
      component: Project
    },
    {
      path: '/preview',
      name: 'Preview',
      meta: { requiresAuth: true },
      component: Preview
    },
    {
      path: '/privacy',
      name: 'Privacy',
      meta: { requiresAuth: true },
      component: Privacy
    },
    {
      path: '/corporate',
      name: 'Corporate',
      meta: { requiresAuth: true },
      component: Corporate
    },
    {
      path: '/evaluation',
      name: 'Evaluation',
      meta: { requiresAuth: true },
      component: Evaluation
    },
    {
      path: '/joinVip',
      name: 'JoinVip',
      meta: { requiresAuth: true },
      component: JoinVip
    },
    {
      path: '/assessment',
      name: 'Assessment',
      meta: { requiresAuth: true },
      component: Assessment
    },
    {
      path: '/newsPage',
      name: 'newsPage',
      meta: { requiresAuth: true },
      component: NewsPage
    },
    {
      path: '/postNews/:post',
      name: 'PostNews',
      meta: { requiresAuth: true },
      component: PostNews
    },
    {
      path: '/talentPage',
      name: 'talentPage',
      meta: { requiresAuth: true },
      component: TalentPage
    },
    {
      path: '/joblist',
      name: 'Joblist',
      meta: { requiresAuth: true },
      component: Joblist
    },
    {
      path: '/companyList',
      name: 'CompanyList',
      meta: { requiresAuth: true },
      component: CompanyList
    },
    {
      path: '/workplacecredit',
      name: 'workplacecredit',
      meta: { requiresAuth: true },
      component: Workplacecredit
    },
    {
      path: '/applyfortherecord',
      name: 'Applyfortherecord',
      meta: { requiresAuth: true },
      component: Applyfortherecord
    },
    {
      path: '/ifocuson',
      name: 'Ifocuson',
      meta: { requiresAuth: true },
      component: Ifocuson
    },
    {
      path: '/information',
      name: 'Information',
      meta: { requiresAuth: true },
      component: Information
    },
    {
      path: '/questionsinfor',
      name: 'Questionsinfor',
      meta: { requiresAuth: true },
      component: Questionsinfor
    },
    {
      path: '/attentionps',
      name: 'Attentionps',
      meta: { requiresAuth: true },
      component: Attentionps
    },
    {
      path: '/myresume',
      name: 'Myresume',
      meta: { requiresAuth: true },
      component: Myresume
    },
    {
      path: '/radarmap',
      name: 'Radarmap',
      meta: { requiresAuth: true },
      component: Radarmap
    },
    {
      path: '/recommendation',
      name: 'Recommendation',
      meta: { requiresAuth: true },
      component: Recommendation
    },
    {
      path: '/companyhomepage',
      name: 'Companyhomepage',
      meta: { requiresAuth: true },
      component: Companyhomepage
    },
    {
      path: '/talents',
      name: 'Talents',
      meta: { requiresAuth: true },
      component: Talents
    },
    {
      path: '/worktlist',
      name: 'Worktlist',
      meta: { requiresAuth: true },
      component: Worktlist
    },
    {
      path: '/issue',
      name: 'Issue',
      meta: { requiresAuth: true },
      component: Issue
    },
    {
      path: '/companydetails',
      name: 'Companydetails',
      meta: { requiresAuth: true },
      component: Companydetails
    },
    {
      path: '/previewLouw',
      name: 'previewLouw',
      meta: { requiresAuth: true },
      component: previewLouw
    },
    {
      path: '/paixielit',
      name: 'Paixielit',
      meta: { requiresAuth: true },
      component: Paixielit
    },
    {
      path: '/myassessment',
      name: 'Myassessment',
      meta: { requiresAuth: true },
      component: Myassessment
    },
    {
      path: '/positionchange',
      name: 'Positionchange',
      meta: { requiresAuth: true },
      component: Positionchange
    },
    {
      path: '/checkitout',
      name: 'Checkitout',
      meta: { requiresAuth: true },
      component: Checkitout
    },
    {
      path: '/collect',
      name: 'Collect',
      meta: { requiresAuth: true },
      component: Collect
    },
    {
      path: '/changes',
      name: 'Changes',
      meta: { requiresAuth: true },
      component: Changes
    },
    {
      path: '/companyUpdata',
      name: 'CompanyUpdata',
      meta: { requiresAuth: true },
      component: CompanyUpdata
    },
    {
      path: '/teamgoum',
      name: 'Teamgoum',
      meta: { requiresAuth: true },
      component: Teamgoum
    },
    {
      path: '/myvip',
      name: 'Myvip',
      meta: { requiresAuth: true },
      component: Myvip
    },
    {
      path: '/Details',
      name: 'Details',
      meta: { requiresAuth: true },
      component: Details
    },
    {
      path: '/whoLookMy',
      name: 'WhoLookMy',
      meta: { requiresAuth: true },
      component: WhoLookMy
    },
    {
      path: '/invite',
      name: 'Invite',
      meta: { requiresAuth: true },
      component: Invite
    },
    {
      path: '/historical',
      name: 'Historical',
      meta: { requiresAuth: true },
      component: Historical
    },
    {
      path: '/addresume',
      name: 'Addresume',
      meta: { requiresAuth: true },
      component: Addresume
    },
    {
      path: '/myReleasePosition',
      name: 'MyReleasePosition',
      meta: { requiresAuth: true },
      component: MyReleasePosition
    },
    {
      path: '/storeBasic',
      name: 'StoreBasic',
      meta: { requiresAuth: true },
      component: StoreBasic
    },
    {
      path: '/answer',
      name: 'Answer',
      meta: { requiresAuth: true },
      component: Answer
    },
    {
      path: '/checkout',
      name: 'checkout',
      meta: { requiresAuth: true },
      component: Checkout,
      beforeEnter: (to, from, next) => {
        if (store.getters.cartChecked.length === 0) {
          if (from.path === '/addresses' || from.path === '/coupons') {
            window.history.length > 2
            ? router.go(-2)
            : router.replace('/')
          } else {
            window.history.length > 1
            ? router.go(-1)
            : router.replace('/')
          }
        } else {
          next()
        }
      }
    },
    {
      path: '/orders',
      name: 'orders',
      meta: { requiresAuth: true },
      component: Order
    },
    {
      path: '/order_goods',
      name: 'orderGoods',
      meta: { requiresAuth: true },
      component: OrderGoods
    },
    {
      path: '/comments/:product',
      name: 'comments',
      meta: { requiresAuth: true },
      component: CommentList
    },
    {
      path: '/comments/:order/:product',
      name: 'comment',
      meta: { requiresAuth: true },
      component: Comment
    }
  ]
})
router.beforeEach((to, from, next) => {
  hackHash()
  initData()
  // 判断来源
  if (to.query.from) {
    localStorage.setItem('from', to.query.from)
  }
  // 登录判断
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.getters.token) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next()
  }
  next()
})
export default router

// ?# 解决微信支付问题
function hackHash () {
  let paths = window.location.href.split('#')
  if (paths[0].charAt(paths[0].length - 1) !== '?') {
    paths[0] = `${paths[0]}?`
  }
  paths[1] = paths[1] || '/'
  let url = `${paths[0]}#${paths[1]}`
  if (window.location.href !== url) {
    window.location.href = url
  }
}

// initData
function initData () {
  if (!store.state.cart.loaded && store.getters.token) {
    store.dispatch('getCartList')
  }
  if (!store.state.address.loaded && store.getters.token) {
    store.dispatch('getAddresses')
  }
  if (!store.getters.userInfo && store.getters.token) {
    store.dispatch('getUserInfo').then((response) => {
      store.dispatch('getUnreadMessage', store.getters.userInfo.id)
    })
  }
}
